/**
 * 
 */
package fr.syntheses.stephaneM.exo.Bean;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

/**
 * @author dev03
 * Nom du projet :
 * Date creation classe :19 sept. 2012
 * fr.syntheses.stephaneM.exo.Bean
 */
public class RecupHtml {

	/**
	 * @param args
	 * @throws IOException 
	 */

	public static String modif(String val)
	{
		String valSortie="";
		valSortie=val.replaceAll("title", "").replaceAll("[\\s\\p{Punct}]", "");
		return valSortie;
	}

	public static void main(String[] args) throws IOException , NullPointerException {
		// TODO Auto-generated method stub
		OutputStreamWriter fos;
		BufferedWriter bos;


		String docSortie=  "";



		// recup url
		URL url =new URL("http://www.clubic.com");
		//transform octet to char
		InputStreamReader t = new InputStreamReader(url.openStream());
		// stocke char dans un buffer qui detecte /n
		BufferedReader b = new BufferedReader(t);

		//String ligne = null;

		//ligne = b.readLine();

		ArrayList<String> al= new ArrayList<String>();

		String tD="<title>";

		String tF="p";
		String curentLine="";

		while (b.readLine()!=null) {

			curentLine = b.readLine(); 

			System.out.println(curentLine);

			if(curentLine.contains("<title>")) {

				System.err.println("youpi !");

				System.err.println(curentLine);

				break;
			}

		}


		docSortie = modif(curentLine);
		System.out.println(docSortie);
		
		fos= new OutputStreamWriter(new FileOutputStream(docSortie));
		while(b.readLine()!=null){
			String ligne=b.readLine();
			al.add(ligne);
			//fos.write(ligne+"\n");
		}
		
		for(int i=0;i<al.size();i++){
			fos.write(al.get(i)+"\n");
		}
		bos=new BufferedWriter(fos);

		fos.close();
		bos.close();




	}
}